%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% File:               aux_log_likelihood2.m
%
% Authors:            Sergio Ascencio and Miguel Rueda
%
% Description:        Log likelihood function for two step ML estimator.

% Language:           MATLAB R2013b (8.2.0.701) 64 Bit
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function [auxLL,Llong]=aux_log_likelihood2(beta,Y_PRI,Y_PAN,X_PRI,X_PAN)

betas=reshape(beta,[size(X_PAN,2),4]);

%Base outcome is L (minimum representation) order of coefficients L, M, H
betas_PRI=[zeros(size(X_PRI,2),1) betas(:,1:2)];
betas_PAN=[zeros(size(X_PAN,2),1) betas(:,3:4)];

y_PAN=dummyvar(Y_PAN+1);
y_PRI=dummyvar(Y_PRI+1);

p_PAN=exp(X_PAN*betas_PAN)./kron(ones(1,size(y_PAN,2)),sum(exp(X_PAN*betas_PAN),2));
p_PRI=exp(X_PRI*betas_PRI)./kron(ones(1,size(y_PRI,2)),sum(exp(X_PRI*betas_PRI),2));

Llong=sum(y_PAN.*log(p_PAN)+y_PRI.*log(p_PRI),2);
auxLL=-sum(sum(y_PAN.*log(p_PAN)+y_PRI.*log(p_PRI),2).^2);

end

